package cpsobras.web;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import cpsobras.material.Material;
import cpsobras.material.MaterialRN;

@ManagedBean(name="materialBean")
@SessionScoped
public class MaterialBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private Material material = new Material();
	private List<Material> lista;
	private List<Material> pesquisarMaterial;
	
	public String novo(){
		this.material = new Material();
		return "material";
	}

	public String salvar(){
		MaterialRN materialRN = new MaterialRN();
		materialRN.salvar(this.material);
		FacesContext context = FacesContext.getCurrentInstance();
		FacesMessage facesMessage = new FacesMessage("Cadastrado/Alterado com sucesso!");
		context.addMessage(null, facesMessage);

		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("materialBean");
		this.material = null;
		return "listar_material";
	}
	
	public String excluir(){
		MaterialRN materialRN = new MaterialRN();
		materialRN.excluir(this.material);
		this.lista = null;
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("materialBean");
		return "listar_material";
	}
		
	public String alterar(){
		return "material";
	}

	public String voltar(){
		return "listar_material";
	}

	public List<Material> getLista() {
		if(this.lista == null){
			MaterialRN materialRN = new MaterialRN();
			this.lista = materialRN.listar();
		}
		return this.lista;
	}

	public Material getMaterial() {
		return material;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}

	public List<Material> getPesquisarMaterial() {
		return pesquisarMaterial;
	}

	public void setPesquisarMaterial(List<Material> pesquisarMaterial) {
		this.pesquisarMaterial = pesquisarMaterial;
	}
}